package fr.cpe.dp.projet2.controller;

import fr.cpe.dp.projet2.factory.GenericFactory;
import fr.cpe.dp.projet2.model.music.IMusic;
import fr.cpe.dp.projet2.model.music.MusicType;
import fr.cpe.dp.projet2.model.musicBuilder.IMusicBuilder;
import fr.cpe.dp.projet2.model.musicPlayer.IMusicPlayer;
import fr.cpe.dp.projet2.view.Step1MusicPlayerView;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.swing.JOptionPane;

/**
 *
 * @author Benoit
 */
public class Step1MusicController {
    private Step1MusicPlayerView musicPlayerView;
    private IMusicPlayer musicPlayer;
    private GenericFactory<IMusic> musicFactory;
    private Map<String,IMusicBuilder> mapMusicBuilders;
    private Map<String,MusicType> mapMusicTypes;

    public Step1MusicController(Step1MusicPlayerView musicPlayerView, GenericFactory<IMusic> musicFactory, 
            IMusicPlayer musicPlayer, Map<String,IMusicBuilder> mapMusicBuilders,
            Map<String,MusicType> mapMusicTypes) {
        this.musicPlayerView = musicPlayerView;
        this.musicPlayer = musicPlayer;
        this.mapMusicBuilders = mapMusicBuilders;
        this.mapMusicTypes = mapMusicTypes;
        this.musicFactory = musicFactory;
        
    }
    
    public void play() {
        try {
            IMusic music = null;
            List<Object> args = new ArrayList<>();
            
            // 1. Premier choix
            String choosenMusicType = musicPlayerView.chooseMusicType();
            MusicType musicType = mapMusicTypes.get(choosenMusicType);
            
            // 2. Deuxième choix
            if(musicType == MusicType.FILE_TYPE) {
                File file = musicPlayerView.chooseFile();
                if(file != null) {
                    args.add(file);
                }
            } else if(musicType == MusicType.GENERATED) {
                String choice = musicPlayerView.chooseMusicBuilder();
                if(mapMusicBuilders.containsKey(choice)) {
                    args.add(mapMusicBuilders.get(choice));
                }
            } else {
                musicType = null;
            }
            
            // 3. Lecture de la musique
            if(musicType == null || args.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Votre saisie est incomplète : impossible de lire une musique.");
            } else {
                music = musicFactory.createObject(musicType.toString(), args);
                musicPlayer.setMusic(music);
                musicPlayer.play();
            }
        } catch (InstantiationException | ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(Step1MusicController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidMidiDataException ex) {
            JOptionPane.showMessageDialog(null, "Le fichier fourni n'est pas au bon format.");
            musicPlayer.stop();
        }
    }
}
